#!/usr/bin/env python
# -*- encoding: latin-1 -*-
#
# [ python-gAlwaysIdle ] is a simple python script to set 'idle' status
# into Gmail chat.
#
#
#  * How to use ?
#    First, you should login to Gmail (gmail.com or Google Apps domain),
#    set your status to 'Invisible' and then, execute this script. Yeah!
#    Now your status is showed as 'idle' in the chat and no more 
#    inconvenient messages to receive! =D
#
#  * Homepage:
#    http://code.google.com/p/python-galwaysidle/
#
#
# ---------------------------------------------------------------------
#                              ,           ,
#                             /             \
#                            ((__-^^-,-^^-__))
#                             `-_---' `---_-'
#                              `--|o` 'o|--'
#                                 \  `  /
#                                  ): :(
#                                  :o_o:
#                                   "-"
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# ---------------------------------------------------------------------
#
#  Author: Ricardo Ichizo <n1ghtcr4wler [at] gmail [dot] com>
#


import os,sys,time
import getpass
import xmpp


config = {  'jid':'',
            'password':'',
            'server':'talk.google.com',
            'port':5222
            }


def away():
    if config['jid'] == '':
        print "Enter your gmail account: ",
        config['jid'] = sys.stdin.readline()[:-1]

    if config['password'] == '':
        config['password'] = getpass.getpass('Enter your password: ')

    jid = xmpp.protocol.JID(config['jid'])
    password = config['password']
    server = config['server']
    port = config['port']

    xmpp_conn = xmpp.Client(jid.getDomain(),debug=[])
    if not xmpp_conn.connect((server,port)):
        raise IOError('Could not connect to server')
    if not xmpp_conn.auth(jid.getNode(),password,'jabber'):
        raise IOError('Could not auth with server')

    xmpp_conn.sendInitPresence()

    presence = xmpp.protocol.Presence(priority=5,show='invisible',status='')
    presence.setShow('invisible')
    presence.setStatus('')
    xmpp_conn.send(presence)

    presence = xmpp.protocol.Presence(priority=5,show='away',status='Auto Idle')
    presence.setShow('away')
    presence.setStatus('Auto Idle')
    xmpp_conn.send(presence)

    try:
        while(1):
            time.sleep(1)
    except KeyboardInterrupt:
        pass

    xmpp_conn.disconnect()
       

if __name__ == '__main__':
    away()

